from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from djiape.entities.models import Company, Person, Currency
from djiape.accounting.models import Account as GlAccount, Journal_type
from django.contrib import admin

class AccountType(models.Model):
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ['name']
        verbose_name = _('account type')
        verbose_name_plural = _('account types')
        
admin.site.register(AccountType)

class BankTransactionType(models.Model):
    TYPE_CHOICES = (
        ('C', _('creditor')),
        ('D', _('debtor')),
    )
    name = models.CharField(_('name'), max_length=30)
    code = models.CharField(_('abbreviature'), max_length=2, unique=True)
    type = models.CharField(_('type'), max_length=1, choices=TYPE_CHOICES)
    consecutive = models.CharField(_('consecutive'), max_length=10, blank=True, null=True)
    journal_type = models.ForeignKey(Journal_type, verbose_name=_('journal type'), blank=True, null=True)
    journal = models.CharField(_('journal'), max_length=10, blank=True, null=True)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["id"]
        verbose_name = _('transaction type')
        verbose_name_plural = _('transaction types')
    
admin.site.register(BankTransactionType)

class Account(models.Model):
    id = models.CharField(_('account'), max_length=20, primary_key=True)
    name = models.CharField(_('name'), max_length=30)
    bank = models.CharField(_('bank'), max_length=30)
    type = models.ForeignKey(AccountType, verbose_name=_('account type'))
    glaccount = models.ForeignKey(GlAccount, verbose_name=_('gl account'))
    check_seq = models.CharField(_('check number'), max_length=10)
    eoper_seq =  models.CharField(_('electronic transaction number'), max_length=10, null=True, blank=True)
    balance = models.DecimalField(_('balance'), max_digits=14, decimal_places=2, editable=False, default=0)
    def __unicode__(self):
        return ('%s : %s') % (self.id, self.name)
    class Meta:
        ordering = ["id"]
        verbose_name = _('account')
        verbose_name_plural = _('accounts')
    
admin.site.register(Account)

class Journal(models.Model):
    account = models.ForeignKey(Account, verbose_name=_('account'))
    fi_operation = models.ForeignKey(BankTransactionType, verbose_name=_('transaction'), related_name='fi_operation')
    folio = models.CharField(_('folio'), max_length=10)
    date = models.DateTimeField(_('date'), auto_now_add=True)
    entity = models.CharField(_('entity'), max_length=15, editable=False, default='')
    fi_currency = models.ForeignKey(Currency, verbose_name=_('currency'), related_name='fi_currency')
    rate = models.DecimalField(_('rate'), max_digits=14, decimal_places=2, default=1)
    total = models.DecimalField(_('total'), max_digits=14, decimal_places=2)
    tax = models.DecimalField(_('tax'), max_digits=14, decimal_places=2)
    credit = models.DecimalField(_('credit'),max_digits=14, decimal_places=2, blank=True, null=True, editable=False)
    debit = models.DecimalField(_('debit'),max_digits=14, decimal_places=2, blank=True, null=True, editable=False)
    beneficiary = models.CharField(_('beneficiary'), max_length=60)
    concept = models.CharField(_('concept'), max_length=60)
    gl_operation = models.IntegerField(_('gl operation'), default=0)
    status = models.CharField(_('status'), max_length=1, default='N')
    fi_user = models.ForeignKey(User, verbose_name=_('user'), related_name='fi_user')
